package com.abel.bigwater.data.jnrq

import com.abel.bigwater.data.BcdString
import com.alibaba.fastjson.JSON
import io.netty.buffer.Unpooled
import org.joda.time.DateTime
import org.junit.Test
import org.slf4j.LoggerFactory
import kotlin.test.assertEquals

class TimingDataTest {

    /**
     * 设置采集周期和上传周期
     */
    @Test
    fun fillBuffer() {
        val buf = Unpooled.buffer()
        val t = TimingData().apply {
            serverTime = DateTime(2020, 3, 31, 12, 26, 29)
            readPeriodStr = byteArrayOf(0, 0, 1, 0)
            uploadPeriodStr = byteArrayOf(0, 0, 12, 0)

            fillBuffer(buf)
        }

        val hex = BcdString.hexString(buf, withBlank = true)
        lgr.info("timing data in hex/json: {} / {}", hex, JSON.toJSONString(t, true))
        assertEquals("00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1d 1a 0c 1f 03 e4 07 00 00 00 0c 00 00 00\r\n01 00 00 00 05 01 00 00 06 01 00 00 ", hex)
    }

    /**
     * 默认采集和上传周期
     */
    @Test
    fun changeDefaultTiming() {
        val buf = Unpooled.buffer()
        val t = TimingData().apply {
            serverTime = DateTime(2020, 3, 31, 12, 26, 29)

            fillBuffer(buf)
        }

        val hex = BcdString.hexString(buf, withBlank = true)
        lgr.info("timing data in hex/json: {} / {}", hex, JSON.toJSONString(t, true))
        assertEquals("00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1d 1a 0c 1f 03 e4 07 00 00 00 0c 00 00 00\r\n01 00 00 00 05 01 00 00 06 01 00 00 ", hex)
    }

    companion object {
        private val lgr = LoggerFactory.getLogger(TimingDataTest::class.java)
    }
}